<?php
require_once(dirname(__FILE__)."/func.php");

$canvas_player_start =
'
<Canvas
	xmlns="http://schemas.microsoft.com/client/2007"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	Width="900" Height="600"
	Background="#FFAAAAAA"
	x:Name="Player">
';

$canvas_player_end = 
'
</Canvas>
';

$canvas_field_start = 
'
	<Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="320" Height="400" Canvas.Left="80" x:Name="Field" Background="#FF2EB009" Canvas.Top="40">
';

$canvas_field_end = 
'
	</Canvas>
';

$canvas_field_struct = 
'
		<Line X1="0" Y1="200" X2="320" Y2="200" Stroke="#FFFDFDFD" StrokeThickness="2" x:Name="CenterLine"/>
		
		<Line X1="0" Y1="0" X2="320" Y2="0" Stroke="#FFFDFDFD" StrokeThickness="2" x:Name="TopLine"/>
		<Line X1="0" Y1="400" X2="320" Y2="400" Stroke="#FFFDFDFD" StrokeThickness="2" x:Name="BottomLine"/>
		<Line X1="0" Y1="0" X2="0" Y2="400" Stroke="#FFFDFDFD" StrokeThickness="2" x:Name="LeftLine" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/>
		<Line X1="320" Y1="0" X2="320" Y2="400" Stroke="#FFFDFDFD" StrokeThickness="2" x:Name="RightLine" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/>
		<Ellipse Width="80" Height="80" Stroke="#FFFDFDFD" StrokeThickness="2" x:Name="CenterCircle" Canvas.Left="120" Canvas.Top="160"/>
		<Line X1="80" Y1="80" X2="240" Y2="80" Stroke="#FFFDFDFD" StrokeThickness="2" x:Name="TopOutHoriz"/>
		<Line X1="240" Y1="0" X2="240" Y2="80" Stroke="#FFFDFDFD" StrokeThickness="2" x:Name="TopOutRight" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/>
		<Line X1="80" Y1="0" X2="80" Y2="80" Stroke="#FFFDFDFD" StrokeThickness="2" x:Name="TopOutLeft" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/>
		<Line X1="120" Y1="0" X2="120" Y2="40" Stroke="#FFFDFDFD" StrokeThickness="2" x:Name="TopInnLeft" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/>
		<Line X1="200" Y1="0" X2="200" Y2="40" Stroke="#FFFDFDFD" StrokeThickness="2" x:Name="TopInnRight" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/>
		<Line X1="120" Y1="40" X2="200" Y2="40" Stroke="#FFFDFDFD" StrokeThickness="2" x:Name="TopInnHoriz"/>
		<Line X1="80" Y1="320" X2="240" Y2="320" Stroke="#FFFDFDFD" StrokeThickness="2" x:Name="BottomOutHoriz"/>
		<Line X1="240" Y1="320" X2="240" Y2="400" Stroke="#FFFDFDFD" StrokeThickness="2" x:Name="BottomOutRight" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/>
		<Line X1="80" Y1="320" X2="80" Y2="400" Stroke="#FFFDFDFD" StrokeThickness="2" x:Name="BottomOutLeft" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/>
		<Line X1="120" Y1="360" X2="120" Y2="400" Stroke="#FFFDFDFD" StrokeThickness="2" x:Name="BottomInnLeft" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/>
		<Line X1="200" Y1="360" X2="200" Y2="400" Stroke="#FFFDFDFD" StrokeThickness="2" x:Name="BottomInnRight" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/>
		<Line X1="120" Y1="360" X2="200" Y2="360" Stroke="#FFFDFDFD" StrokeThickness="2" x:Name="BottomInnHoriz"/>
		<Path x:Name="TopHalfCurve" Width="80" Height="80" Stretch="Fill" Stroke="#FFFFFFFF" StrokeThickness="2" Data="M79,40 C79,61.539105 61.539105,79 40,79 C18.460895,79 1,61.539105 1,40 C1,18.460895 18.460895,1 40,1 C61.539105,1 79,18.460895 79,40 z" Clip="M0,40 L80,40 L80,80 L0,80 z" Canvas.Left="120" Canvas.Top="40"/>
		<Ellipse Width="80" Height="80" Stroke="#FFFFFFFF" StrokeThickness="2" x:Name="BottomHalfCurve" Clip="M0,0 L80,0 L80,40 L0,40 z" Canvas.Left="120" Canvas.Top="280"/>
		<Ellipse x:Name="TopPenalty" Width="4" Height="4" Fill="#FFFFFFFF" Canvas.Left="158" Canvas.Top="59"/>
		<Ellipse x:Name="BottomPenalty" Width="4" Height="4" Fill="#FFFFFFFF" Canvas.Left="158" Canvas.Top="341"/>
';

$canvas_player_control = 
'
	<Canvas Width="160" Height="40" Canvas.Top="450" x:Name="PlayBar" Visibility="Visible" Canvas.Left="80">
		<Canvas Width="20" Height="20" Canvas.Left="90" Canvas.Top="10" x:Name="PlayerPause" MouseLeftButtonDown="onPause" Background="#FFE8E9F0" Canvas.ZIndex="1">
			<Polygon Points="0,0 0,20 20,20 20,0" Stroke="Black" StrokeThickness="1" FillRule="EvenOdd"/>
			<Rectangle Width="6" Height="14" Fill="#FFFFFFFF" Stroke="#FF000000" x:Name="PauseLeft" Canvas.Left="3" Canvas.Top="3"/>
			<Rectangle Width="6" Height="14" Fill="#FFFFFFFF" Stroke="#FF000000" Canvas.Left="11" x:Name="PauseRight" Canvas.Top="3"/>
		</Canvas>
		<Canvas Width="20" Height="20" Canvas.Left="130" Canvas.Top="10" x:Name="PlayerResume" MouseLeftButtonDown="onResume" Background="#FFD9D8DA" Canvas.ZIndex="1">
			<Polygon Points="0,0 0,20 20,20 20,0" Stroke="Black" StrokeThickness="1"/>
			<Rectangle Width="6" Height="14" Fill="#FFFFFFFF" Stroke="#FF000000" x:Name="ResumeLeft" Canvas.Top="3" Canvas.Left="3"/>
			<Path Width="6" Height="12" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" Data="M21.75,110.75 L21.75,120.5 26.166667,115.66667 z" Canvas.Left="11" x:Name="ResumeRight" Canvas.Top="4"/>
		</Canvas>
		<Canvas x:Name="PlayerPlay" Width="20" Height="20" Canvas.Left="10" Canvas.Top="10" MouseLeftButtonDown="onPlay" Background="#FFD9D8DA" Canvas.ZIndex="1">
			<Polygon Points="0,0 0,20 20,20 20,0" Stroke="Black" StrokeThickness="1"/>
			<Path Width="14" Height="16" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" Data="M371.66667,12 L371.66667,27.5 385.16632,20.333333 z" x:Name="PlayTri" Canvas.Left="3" Canvas.Top="2"/>
		</Canvas>
		<Canvas Width="20" Height="20" Canvas.Left="50" Canvas.Top="10" x:Name="PlayerStop" MouseLeftButtonDown="onStop" Background="#FFD9D8DA" Canvas.ZIndex="1">
			<Polygon Points="0,0 0,20 20,20 20,0" Stroke="Black" StrokeThickness="1"/>
			<Rectangle Width="14" Height="14" Fill="#FFFFFFFF" Stroke="#FF000000" x:Name="StopButton" Canvas.Left="3" Canvas.Top="3"/>
		</Canvas>
		<Rectangle x:Name="pb_bg" Width="160" Height="40" Canvas.Left="0" Canvas.Top="0" RadiusX="4" RadiusY="4" Opacity="1" Canvas.ZIndex="0" Stroke="#FF000000" Fill="#FFFFFFFF"/>
	</Canvas>
';

/* 
 * un=user1,user2,user3
 * no=10,11,9
 * nl=100,100,100 (number's left)
 * nt=100,100,100 (number's top)
 * nw=100,100,100 (number's width)
 * nh=100,100,100 (number's height)
 * ult=(100,100)-(100,200)-(100,300)
 * pn=path1,,path3
 * pp=(100,200 100,300 300,400)-(100,200 200,100)         -- Canvas.Left,Canvas.Top
 * pt=(00:00:00,00:00:01,00:00:02)-(00:00:00,00:00:02)
 * pf=(09fd,fdifdif,fff)_(ff,ff)
 * sn=example
 */
$sn = isset($_GET["sn"]) ? $_GET["sn"] : "example";
$un = isset($_GET["un"]) ? $_GET["un"] : "";
$no = isset($_GET["no"]) ? $_GET["no"] : "";
$nl = isset($_GET["nl"]) ? $_GET["nl"] : "";
$nt = isset($_GET["nt"]) ? $_GET["nt"] : "";
$nw = isset($_GET["nw"]) ? $_GET["nw"] : "";
$nh = isset($_GET["nh"]) ? $_GET["nh"] : "";
$ult = isset($_GET["ult"]) ? $_GET["ult"] : "";
$pn = isset($_GET["pn"]) ? $_GET["pn"] : "";
$pp = isset($_GET["pp"]) ? $_GET["pp"] : "";
$pt = isset($_GET["pt"]) ? $_GET["pt"] : "";
$pf = isset($_GET["pf"]) ? $_GET["pf"] : "";

$userNames = split(",", $un);
$userNumbers = split(",", $no);
$nls = split(",", $nl);
$nts = split(",", $nt);
$nws = split(",", $nw);
$nhs = split(",", $nh);
$userLeftTops = split("-", $ult);
$pathNames = split(",", $pn);
$pathPoints = split("-", $pp);
$pathTimes = split("-", $pt);
$pointFills = split("_", $pf);

$users = array();
$paths = array();
$points = array();

$point_idx = 0;

for ($i = 0; $i < count($userNames); $i++) {
	$user_name = trim($userNames[$i]);
	$user_number = trim($userNumbers[$i]);
	$u_nl = $nls[$i];
	$u_nt = $nts[$i];
	$u_nw = $nws[$i];
	$u_nh = $nhs[$i];
	
	$user_lt = $userLeftTops[$i];
	$user_lt = substr($user_lt, 1, strlen($user_lt) - 2);
	$user_lt = split(",", $user_lt);
	if ($pathNames[$i] == null) {
		if ($user_name != "Ball")
			$user = 
'
<Canvas Width="30" Height="40" Canvas.Left="'.$user_lt[0].'" Canvas.Top="'.$user_lt[1].'" x:Name="'.$user_name.'" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MouseLeftButtonDown="userLeftDown"><Ellipse Fill="#FFFFFFFF" Stroke="#FF000000" Width="8" Height="8" Canvas.Left="11" Canvas.Top="0" StrokeThickness="0.5"/><Rectangle Fill="#FFFFFFFF" Stroke="#FF000000" StrokeThickness="0.5" Width="14" Height="24" Canvas.Left="8" Canvas.Top="8"/><Rectangle Fill="#FFFFFFFF" Stroke="#FF000000" StrokeThickness="0.5" Width="6" Height="15" Canvas.Top="8" Canvas.Left="2"/><Rectangle Fill="#FFFFFFFF" Stroke="#FF000000" StrokeThickness="0.5" Width="6" Height="8" Canvas.Top="32" Canvas.Left="8"/><Rectangle Fill="#FFFFFFFF" Stroke="#FF000000" StrokeThickness="0.5" Width="6" Height="15" Canvas.Left="22" Canvas.Top="8"/><Rectangle Fill="#FFFFFFFF" Stroke="#FF000000" StrokeThickness="0.5" Width="6" Height="8" Canvas.Left="16" Canvas.Top="32"/><TextBlock Width="'.$u_nw.'" Height="'.$u_nh.'" Text="'.$user_number.'" TextWrapping="Wrap" FontSize="8" Canvas.Top="'.$u_nt.'" Canvas.Left="'.$u_nl.'" FontWeight="Bold" Foreground="#FFFF0000" FontStretch="Normal"/></Canvas>
';
			/*$user = 
'
		<Ellipse Width="24" Height="30" Fill="#FFFFFFFF" Stroke="#FF000000" x:Name="'.$user_name.'" Visibility="Visible" Canvas.Left="'.$user_lt[0].'" Canvas.Top="'.$user_lt[1].'"/>
';*/
		else
			$user =
'
        <Ellipse Width="8" Height="8" Fill="#FFFFFFFF" Stroke="#FF000000" Canvas.Left="'.$user_lt[0].'" Canvas.Top="'.$user_lt[1].'" x:Name="Ball"/>
';
		$users[] = $user;
	} else {
		$path_name = $pathNames[$i];
		$path_points = $pathPoints[$i];
		$path_points = substr($path_points, 1, strlen($path_points) - 2);
		$point_fills = $pointFills[$i];
		$point_fills = substr($point_fills, 1, strlen($point_fills) - 2);
		$path_times = $pathTimes[$i];
		$path_times = substr($path_times, 1, strlen($path_times) - 2);
		
		$path_points = split(" ", $path_points);
		$point_fills = split(",", $point_fills);
		$path_times = split(",", $path_times);
		
		if ($user_name != "Ball")
			$user = 
'
<Canvas Width="30" Height="40" Canvas.Left="'.$user_lt[0].'" Canvas.Top="'.$user_lt[1].'" x:Name="'.$user_name.'" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MouseLeftButtonDown="userLeftDown"><Canvas.Triggers><EventTrigger RoutedEvent="Canvas.Loaded"><BeginStoryboard><Storyboard x:Name="GoalVideo'.$i.'">';

			/*$user = 
'
		<Ellipse Width="24" Height="30" Fill="#FFFFFFFF" Stroke="#FF000000" x:Name="'.$user_name.'" Visibility="Visible"  Canvas.Left="'.$user_lt[0].'" Canvas.Top="'.$user_lt[1].'">
			<Ellipse.Triggers>
	    		<EventTrigger RoutedEvent="Ellipse.Loaded">
	    			<BeginStoryboard>
	    				<Storyboard x:Name="GoalVideo'.$i.'">';*/
	    else
	    	$user =
'
        <Ellipse Width="8" Height="8" Fill="#FFFFFFFF" Stroke="#FF000000" Canvas.Left="'.$user_lt[0].'" Canvas.Top="'.$user_lt[1].'" x:Name="'.$user_name.'">
			<Ellipse.Triggers>
	    		<EventTrigger RoutedEvent="Ellipse.Loaded">
	    			<BeginStoryboard>
	    				<Storyboard x:Name="GoalVideo'.$i.'">';
	    
	    $top_part = '';
	    $left_part = '';
		$points_attr = '';
	    for ($j = 0; $j < count($path_points); $j++) {
	   		$tmp_point = $path_points[$j];
	   		$tmp_point = split(",", $tmp_point);
	   		$point_left = $tmp_point[0];
	   		$point_top = $tmp_point[1];
	   		
	   		$tmp_fill = $point_fills[$j];
			$tmp_time = $path_times[$j];
			
			if ($j == 0) {
				$top_part .= 
'							<DoubleAnimationUsingKeyFrames BeginTime="'.$tmp_time.'" Storyboard.TargetName="'.$user_name.'" Storyboard.TargetProperty="(Canvas.Top)">';
				$left_part .= 
'	    					<DoubleAnimationUsingKeyFrames BeginTime="'.$tmp_time.'" Storyboard.TargetName="'.$user_name.'" Storyboard.TargetProperty="(Canvas.Left)">';
			}
			
			$hh = $user_name == "Ball" ? 4 : 20;
			$hw = $user_name == "Ball" ? 4 : 15;
			$top_part .=
'								<SplineDoubleKeyFrame KeyTime="'.$tmp_time.'" Value="'.($point_top + 4 - $hh).'"/>';
			$left_part .=
'								<SplineDoubleKeyFrame KeyTime="'.$tmp_time.'" Value="'.($point_left + 4 - $hw).'"/>';

			// Push the points
			$point = 
'		<Ellipse x:Name="spot'.$point_idx.'" Width="8" Height="8" Fill="'.SL_GetColor($tmp_fill).'" Canvas.Left="'.$point_left.'" Canvas.Top="'.$point_top.'" Canvas.ZIndex="1" Stroke="#FFA2E1F4" StrokeThickness="1"/>';

			$points_attr .= ($point_left + 4).",".($point_top + 4)." ";
			$points[] = $point;
			$point_idx++;
		}
		$top_part .= 
'
							</DoubleAnimationUsingKeyFrames>
';
		$left_part .= 
'
							</DoubleAnimationUsingKeyFrames>
';

		$user .= $top_part;
		$user .= $left_part;
		if ($user_name != "Ball")
	    	$user .=
'						</Storyboard>
					</BeginStoryboard>
				</EventTrigger>
			</Canvas.Triggers>
			<Ellipse Fill="#FFFFFFFF" Stroke="#FF000000" Width="8" Height="8" Canvas.Left="11" Canvas.Top="0" StrokeThickness="0.5"/><Rectangle Fill="#FFFFFFFF" Stroke="#FF000000" StrokeThickness="0.5" Width="14" Height="24" Canvas.Left="8" Canvas.Top="8"/><Rectangle Fill="#FFFFFFFF" Stroke="#FF000000" StrokeThickness="0.5" Width="6" Height="15" Canvas.Top="8" Canvas.Left="2"/><Rectangle Fill="#FFFFFFFF" Stroke="#FF000000" StrokeThickness="0.5" Width="6" Height="8" Canvas.Top="32" Canvas.Left="8"/><Rectangle Fill="#FFFFFFFF" Stroke="#FF000000" StrokeThickness="0.5" Width="6" Height="15" Canvas.Left="22" Canvas.Top="8"/><Rectangle Fill="#FFFFFFFF" Stroke="#FF000000" StrokeThickness="0.5" Width="6" Height="8" Canvas.Left="16" Canvas.Top="32"/><TextBlock Width="'.$u_nw.'" Height="'.$u_nh.'" Text="'.$user_number.'" TextWrapping="Wrap" FontSize="8" Canvas.Top="'.$u_nt.'" Canvas.Left="'.$u_nl.'" FontWeight="Bold" Foreground="#FFFF0000" FontStretch="Normal"/>
		</Canvas>
';
		else
			$user .=
'						</Storyboard>
					</BeginStoryboard>
				</EventTrigger>
			</Ellipse.Triggers>
		</Ellipse>
';

		// Push the user
		$users[] = $user;
		
		// Push the path
		$path = 
'
		<Polyline x:Name="'.$path_name.'" Points="'.trim($points_attr).'" Stroke="#FFC4DFF9" StrokeThickness="4" Canvas.Left="0" Canvas.Top="0" Canvas.ZIndex="1" Opacity="0.4"/>
';
		$paths[] = $path;
	}
}

/*$user = 
'
		<Ellipse Width="24" Height="30" Fill="#FFFFFFFF" Stroke="#FF000000" x:Name="No9" Visibility="Visible" Canvas.Left="10" Canvas.Top="10">
			<Ellipse.Triggers>
	    		<EventTrigger RoutedEvent="Ellipse.Loaded">
	    			<BeginStoryboard>
	    				<Storyboard x:Name="GoalVideo0">
							<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="No9" Storyboard.TargetProperty="(Canvas.Top)">
								<SplineDoubleKeyFrame KeyTime="00:00:00" Value="100"/>
								<SplineDoubleKeyFrame KeyTime="00:00:01" Value="200"/>
								<SplineDoubleKeyFrame KeyTime="00:00:02" Value="100"/>
							</DoubleAnimationUsingKeyFrames>
	    					<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="No9" Storyboard.TargetProperty="(Canvas.Left)">
	    						<SplineDoubleKeyFrame KeyTime="00:00:00" Value="100"/>
								<SplineDoubleKeyFrame KeyTime="00:00:01" Value="200"/>
								<SplineDoubleKeyFrame KeyTime="00:00:02" Value="300"/>
	    					</DoubleAnimationUsingKeyFrames>
						</Storyboard>
					</BeginStoryboad>
				</EventTrigger>
			</Ellipse.Triggers>
		</Ellipse>
';*/

/*$path = 
'
		<Polyline x:Name="path1" Points="100,100 200,200 300,100" Stroke="#FFC4DFF9" StrokeThickness="4" Canvas.Left="0" Canvas.Top="0" Canvas.ZIndex="1" Opacity="0.4"/>
';*/

/*$point = 
'
		<Ellipse x:Name="spot1" Width="8" Height="8" Fill="#FFA2E1F4" Canvas.Left="96" Canvas.Top="96" Canvas.ZIndex="1" Stroke="#FFA2E1F4" StrokeThickness="1"/>
		<Ellipse x:Name="spot2" Width="8" Height="8" Fill="#FFA2E1F4" Canvas.Left="196" Canvas.Top="196" Canvas.ZIndex="1" Stroke="#FFA2E1F4" StrokeThickness="1"/>
		<Ellipse x:Name="spot3" Width="8" Height="8" Fill="#FFA2E1F4" Canvas.Left="296" Canvas.Top="96" Canvas.ZIndex="1" Stroke="#FFA2E1F4" StrokeThickness="1"/>
';*/

//$xaml = $canvas_player_start.$canvas_field_start.$canvas_field_struct;
$xaml = $canvas_field_start.$canvas_field_struct;

foreach ($users as $u)
	$xaml .= $u;
foreach ($paths as $p)
	$xaml .= $p;
foreach ($points as $pt)
	$xaml .= $pt;
//$xaml .= $canvas_field_end.$canvas_player_control.$canvas_player_end;
$xaml .= $canvas_field_end;

$h = fopen("data/".$sn.'.xaml', 'w');
fwrite($h, $xaml);
fclose($h);
?>